<?php
namespace app\index\controller;

use app\common\model;
use think\Cache;
use think\Controller;

class Base extends Controller
{
    public $member_id = 0;
    public $member_name = "";
    public $baseRetrunData = [];
    public $base_module = "";
    public $base_controller = "";
    public $base_action = "";
    public $base_request_url = "";
    public $allow_project_ids=[];
    /**
     * 无需验证权限地址
     */
    public $access_url=array("/","/index/home/index","/index/home","/index");
    public function _initialize()
    {
        parent::_initialize();
        
        $base_menu = model("Menu");
        //  $base_menu->select();
        $menu_list = unserialize(Cache::get("cache_menu"));
        if (empty($menu_list)) {
            $menu_list = $base_menu->all()->toArray();
            Cache::set("cache_menu", strtolower(serialize($menu_list)));
        }
        if(session("role_id")!=1)
        {
            $menu_list=get_user_menu(0);
        }else{
            session("user_menu",strtolower(serialize($menu_list)));
        }
        $allow_project_ids=session("allow_project_ids");
       
        $request = request()->instance();
        $this->base_module = strtolower($request->module());
        $this->base_controller = strtolower($request->controller());
        $this->base_action = strtolower($request->action());
        $data["base_controller"] = $this->base_controller;
        $this->member_id=session("user_id");
        if(empty($this->member_id)){
            session("user_id",null);
            session("user_menu",null);
            session_destroy();
            $this->redirect(url("Index/common/login"));        

        }   
        $choose_menu=[];
        foreach ($menu_list as $key => $mm) {
            $url_str = str_replace("/", ".", $mm["url"]);

            $bb = strtolower($this->base_controller);
            $choose_menu = 0;
            $url_str = strtolower($url_str);

            if (stripos($url_str, $bb)) {
                $menu_list[$key]["active"] = 1;
                $choose_menu = $mm;
                if ($mm["p_id"] == 0) {
                    $choose_menu = $mm["id"];
                } else {
                    $choose_menu = explode(",", $mm["p_path"])[2];
                    break;
                }
            } else {
                $menu_list[$key]["active"] = 0;

            }
        }

        $this->base_request_url = "/" . str_replace(".", "/", $this->base_module) . "/" . str_replace(".", "/", $this->base_controller) . "/" . str_replace(".", "/", $this->base_action);
  
        if(!in_array($this->base_request_url,$this->access_url)){
        $check= check_access( $this->base_request_url );
     
        if(empty($check)&&session("role_id")!=1){
           $this->error("无权访问",url("/index/home/index"));
        }
    }
        $curr_menu = $base_menu->get_menu($this->base_request_url);
        $data["child_menu_list"] = [];

        if (!empty($curr_menu)) {
            if ($curr_menu["level"] == 2) {
                $data["child_menu_list"] = $base_menu->get_child_id($curr_menu["id"]);
            }
            if ($curr_menu["level"] == 3) {
                $data["child_menu_list"] = $base_menu->get_brother_item($curr_menu);
                if(!empty($data["child_menu_list"] )){
                    $p_menu=$base_menu->get_menu_id($data["child_menu_list"][0]["p_id"]);
                    array_push($data["child_menu_list"], $p_menu);
                }
                
            }
           $menu_ids=array_column(  $data["child_menu_list"],"id");
           if(!in_array($curr_menu["id"],$menu_ids)){
             array_push($data["child_menu_list"], $curr_menu);
           }
           
            
        }
        $user_role_item=model("Role")->where(["id"=>intval(session("role_id"))])->find();
         //  print_r($allow_project_ids);
        //检查项目权限
        if(empty($allow_project_ids)){
            $user_role_item=model("Role")->where(["id"=>intval(session("role_id"))])->find();
            $allow_project=0;
            if(!empty($user_role_item)){
                $allow_project=$user_role_item->is_project;           
            }
            if($allow_project==0){
               $project_list= model("Team")->where(["user_id"=>  $this->member_id])->select()->toArray();
               $allow_project_ids= array_column($project_list,"project_id");
               array_push($allow_project_ids,-1);
               session("allow_project_ids",$allow_project_ids);
            }else{
                session("allow_project_ids",999);
                $allow_project_ids=999;
            }          
        }
        $this->allow_project_ids= $allow_project_ids;
        
        $data["curr_menu_id"] = 0;
        // print_r(  $data["child_menu_list"]);
        if(!empty($curr_menu["id"])){
            $data["curr_menu_id"] = $curr_menu["id"];
        }
     
        $data["choose_menu"] = $choose_menu;
        $data["base_action"] = $this->base_action;
        $data["page_url"] = $this->base_request_url;
        $data["base_menu_list"] = array_to_tree($menu_list, 2);
        $this->assign($data);

    }

    /**
     *  添加、修改公共
     */
    public function operateData($data, $map = [], $model = null,$validate=false)
    {
        $result = 0;
        if (!empty($model)) {
            $opModel = $model;
        } else {
            if (!isset($this->tableName)) {
                return $result;
            }
            $opModel = model($this->tableName);

        }
        if (!empty($map)) {
            $info = $opModel->where($map)->find();
            if (empty($info)) {
                return $result;
            }
            $data["id"] = $info["id"];
            $result = $opModel->where($map)->update($data);
        } else {
            $result = $opModel->save($data);
        }
        $this->add_log($this->base_request_url);
        // $action_utl=
        //$this->add_log("","",)
        return $result;

    }

    public function delBase($map)
    {

        $model = model($this->tableName);
        $this->add_log($this->base_request_url);
        $result = $model->where($map)->delete();
        return $result;
    }

    /**
     *  数据分页
     */
    public function page($where = [], $page_count = 10, $_tableName = "", $order = "", $_with = "",$group="",$filed="*")
    {

        if (empty($_tableName)) {
            $_tableName = $this->tableName;
        }

        $model = model($_tableName);
        if (!empty($where)) {
            $model = $model->where($where);
        }
        if (!empty($group)) {
            $model = $model->group($group);
        }
        if (!empty($_with)) {
            $model = $model->with($_with);
        }
        if (empty($order)) {
            $order = "id desc";
        }
        $data = [];
        if (empty($_tableName)) {
            return $data;
        }
        $this->add_log($this->base_request_url);

        $data_list = $model->field($filed)->order($order)->paginate($page_count);
        $data["data_list"] = $data_list;
        $page = $data_list->render();
        $data["page"] = $page;
        $this->assign($data);
        return $data;

    }
    /**
     * 插入系统日志
     */
    public function add_log($action, $content = "", $title = "", $mtype = "", $remark = "")
    {
        $request = request();
        $action=strtolower($action);
        $logDada["content"] = $content;
        $menu=model("Menu")->where(["url"=>$action])->find();
        if(!empty($menu)){
            $title=$menu->title;
        }
        $logDada["title"] = $title;
        $logDada["mtype"] = $mtype;
        $logDada["action"] = $action;
        $logDada["uid"] = $this->member_id;
        $logDada["action"] = $action;
        $logDada["ip"] = $request->ip();
        $logDada["remark"] = $remark;
        $logModel = model("SystemLog");
        $logModel->save($logDada);
    }
}
